import requests,json

# 地理编码

def geocode(key,address):
    """将详细的结构化地址转换为高德经纬度坐标"""
    geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key": key,
        "address": address,
        "output": "json"
    }
    r = requests.get(geocode_url, params=params)
    return r.json()


# 逆地理编码

def re_geocode(key,location):
    """将经纬度转换为详细结构化的地址，且返回附近周边的POI、AOI信息"""
    re_geocode_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {"key": key,
              "location": location,
              "output": "json"
             }
    r = requests.get(re_geocode_url, params=params)
    return r.json()


# 步行路径规划

def walking(key,origin,destination):
    """可以规划100KM以内的步行通勤方案，并且返回通勤方案的数据"""
    walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "output": "json"
    }
    r = requests.get(walking_url,params=params)
    return r.json()


# 公交路径规划

def bus(key,origin,destination,city):
    """规划综合各类公共（火车、公交、地铁）交通方式的通勤方案，并且返回通勤方案的数据"""
    bus_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "city":city,
        "output": "json"
    }
    r = requests.get(bus_url, params=params)
    return r.json()


# 驾车路径规划

def drive(key,origin,destination,extensions="base"):
    """规划以小客车、轿车通勤出行的方案，并且返回通勤方案的数据"""
    drive_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "extensions": extensions,
        "output": "json"
    }
    r = requests.get(drive_url, params=params)
    return r.json()


# 骑行路径规划

def cycle(key,origin,destination):
    """规划骑行通勤方案，规划时不会考虑路况；考虑天桥、单行线、封路等情况"""
    cycle_url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
    }
    r = requests.get(cycle_url, params=params)
    return r.json()